<template>
	<div>
		websocket 测试</br>
		<button @click="login">登录</button>
		<button @click="loginOut">登出</button>
		<button @click="sendSingleMessage">单发消息</button>
		<button @click="getOnlineUsers">在线联系人</button>
		<button @click="sendRoomMessage">会议群发消息</button>
	</div>
</template>

<script>
	let tvvssdk;
	let initFlag = false;
	export default {
		name: "Test",
		data() {
			return {
				onlineUsers: [],
			};
		},
		methods: {
			login: function() {
				let _this = this;
				tvvssdk.login("jack", "1", function(res) {
					if (res.success) {
						console.log("登录成功");
						_this.setMessageCallback();
					} else {
						console.log("登录失败,原因:" + res.msg);
					}
				});


			},
			loginOut: function() {
				tvvssdk.loginOut();
			},
			sendSingleMessage: function() {
				tvvssdk.sendSingleMessage("joker", "1", "test", "123456", function(res) {
					console.log(res);
				});
			},
			setMessageCallback: function() {
				let callbacks = {
					onmessage: function(sender, senderPlatform, operation, content) {
						console.log("发送者:" + sender + ",发送者平台:" + senderPlatform + ",操作:" + operation + ",内容:" + content);
					},
					repeatlogin: function() {
						alert("您的账号在其他地方登录");
						location.reload();
					}
				};
				tvvssdk.addMessageEventListener(callbacks)
			},
			getOnlineUsers: function() {
				tvvssdk.getOnlineUsers(function(res) {
					if (res.success) {
						console.log("在线联系人", res.data);
					} else {
						console.log(res.msg);
					}

				});
			},
			sendRoomMessage: function() {
				alert("send")
				tvvssdk.sendRoomMessage(1056, "test", "123456", function(res) {
					console.log(res);
					if (!res.success) {
						alert(res.msg);
					}
				});
			}
		},
		created() {
			tvvssdk = this.$tvvssdk;
			let options = {
				camerares: "720P"
			};
			tvvssdk.init(options, function(res) {
				initFlag = res.success;
				if (!res.success) {
					alert(res.msg);
				}
			});

		}
	};
</script>

<style></style>
